package com.getpebble.android.main.sections.notifications;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ObjectUtil;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import java.util.ArrayList;

public class CannedResponsesFragment extends PblBaseFragment {
    private static final String TAG = CannedResponsesFragment.class.getSimpleName();
    private ArrayList<String> mCannedResponses = new ArrayList();
    private EditText[] mEditTexts = new EditText[5];
    private final SharedPreferences mSharedPrefs = PblPreferences.getSharedPreferences(PebbleApplication.getAppContext());

    public int getLayoutId() {
        return R.layout.fragment_canned_responses;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout mainView = (LinearLayout) container.findViewById(R.id.screen_layout);
        for (int i = 0; i < 5; i++) {
            String storedResponse = this.mSharedPrefs.getString("canned_response_" + i, null);
            Trace.verbose(TAG, "Loading response " + i + " from prefs = '" + storedResponse + "'");
            this.mCannedResponses.add(storedResponse);
            View responseItem = inflater.inflate(R.layout.canned_response_item, container, false);
            this.mEditTexts[i] = (EditText) responseItem.findViewById(R.id.response);
            this.mEditTexts[i].setText(storedResponse);
            ((TextView) responseItem.findViewById(R.id.number)).setText(String.valueOf(i + 1));
            mainView.addView(responseItem);
        }
    }

    public void onPause() {
        for (int i = 0; i < 5; i++) {
            String newVal = this.mEditTexts[i].getText().toString();
            String oldVal = (String) this.mCannedResponses.get(i);
            if (!ObjectUtil.nullCheckEquals(oldVal, newVal)) {
                Trace.verbose(TAG, "Canned response i old = '" + oldVal + "' new = '" + newVal + "'");
                this.mSharedPrefs.edit().putString("canned_response_" + i, newVal).commit();
            }
        }
        super.onPause();
    }
}
